package com.it.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.reggie.Service.EmployeeService;
import com.it.reggie.common.R;
import com.it.reggie.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {



    @Autowired
    private EmployeeService employeeService;

    /**
     * 判断是否登录成功
     *
     * @param request 获取会话 登录成功传递id
     * @param employeReq 登录访问值
     * @return 结果集
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employeReq) {

        //1.判断用户名是否存在
        Employee employee = employeeService.UserNameLoing(employeReq.getUsername());

        if (employee == null){
            return R.error("用户名不存在");
        }

        //2.登录传递密码进行md5加密
        String password = employeReq.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        System.out.println(password);
        //3.比对密码 ，如果不一致返回登录失败
        if (!employee.getPassword().equals(password)){
            return R.error("密码错误！请重新输入");
        }

        //4.密码正确，判断当前用户状态是否可用
        if (employee.getStatus() == 0){
            return R.error("当前状态不可用");
        }
        //5.登录成功 用户id写入Session
        request.getSession().setAttribute("employee",employee.getId());
        return R.success(employee);
    }

    /**
     * 用户退出方法
     * @param request 设置Session
     * @return 结果集
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){

        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee 员工信息
     * @return 结果集
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("员工信息 {}",employee);

        //设置初始密码 进行md5加密
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        //设置创建时间 更新时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //设置创建人
        //获取创建人id 也就是登录id
        //Long CreateUser = (Long) request.getSession().getAttribute("employee");
        //设置创建人 id
        //employee.setCreateUser(CreateUser);
        //employee.setUpdateUser(CreateUser);

        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> Paging (int page,int pageSize,String name){
        log.info("参数 page {} pageSize {} name {}",page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);

        //排序条件
        lambdaQueryWrapper.orderByDesc(Employee:: getUpdateTime);

        //执行查询
        employeeService.page(pageInfo, lambdaQueryWrapper);
        pageInfo.setTotal(employeeService.selectTotalCount());
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @return
     */
    @PutMapping
    public R<String> updata(HttpServletRequest servletRequest, @RequestBody Employee employee){

        log.info(employee.toString());

        //修改人id
        //Long UserId = (Long) servletRequest.getSession().getAttribute("employee");
        //修改时间
        //employee.setUpdateTime(LocalDateTime.now());
        //修改人
        //employee.setUpdateUser(UserId);
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }


//    http://127.0.0.1:8080/employee/1621261923704115201
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);

        if (byId!=null){
            return R.success(byId);
        }
        return R.error("没有查到对应员工信息");
    }


}
